package com.jh3502.vivo.utils;

import android.os.Environment;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;

/**
 * @author harisucici
 * @date 2017/7/7
 */

public class SdCard {
    // 分隔符
    public final static String FILE_EXTENSION_SEPARATOR = ".";
    private static final String TAG = "SdCard";
    private final static String FOLDER = "/3502helper"; // 根目录
    private final static String IMG = "/img"; // 图片目录
    private final static String VIDEO = "/video"; // 视频目录
    private final static String AUDIO = "/audio"; // 音频目录
    private final static String DOWNLOAD = "/download"; // 下载目录
    private final static String TMP = "/tmp"; // 临时目录
    private final static String LOG = "/log"; // 日志
    private final static String HEADFILE = DOWNLOAD + "/headfile"; //头像文件
    private static SdCard sdCard;

    public static synchronized SdCard getInstance() {
        if (sdCard == null) {
            sdCard = new SdCard();
        }
        return sdCard;
    }

    @NonNull
    public boolean hasSdcard() {
        String status = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(status);
    }

    @NonNull
    public String getRootPath() {
        File file = new File(Environment.getExternalStorageDirectory().toString() + FOLDER);
        if (!file.exists()) {
            file.mkdirs();
        }
        return Environment.getExternalStorageDirectory().toString() + FOLDER;
    }

    @NonNull
    //获得不带扩展名的文件名称
    public String getFileNameWithoutExtension(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return filePath;
        }
        int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR);
        int filePosi = filePath.lastIndexOf(File.separator);
        if (filePosi == -1) {
            return (extenPosi == -1 ? filePath : filePath.substring(0,
                    extenPosi));
        }
        if (extenPosi == -1) {
            return filePath.substring(filePosi + 1);
        }
        return (filePosi < extenPosi ? filePath.substring(filePosi + 1,
                extenPosi) : filePath.substring(filePosi + 1));
    }

    @NonNull
    public String getImg() {
        File file = new File(getRootPath() + IMG);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + IMG;
    }


//    public void setImg(String fileName, String url){
//        Glide.with().load(url).asBitmap().toBytes().into(new SimpleTarget<byte[]>() {
//            @Override
//            public void onResourceReady(byte[] bytes, GlideAnimation<? super byte[]> glideAnimation) {
//                try {
//                    savaFileToSD(fileName,bytes);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    public void setFile(String filename, byte[] bytes) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + FOLDER;
            File dir1 = new File(filePath);
            if (!dir1.exists()) {
                dir1.mkdirs();
            }
//            filename = filePath+ "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(bytes);
            //将bytes写入到输出流中
            output.close();
            //关闭输出流
        }
    }

    @NonNull
    public String getVideo() {
        File file = new File(getRootPath() + VIDEO);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + VIDEO;
    }

    @NonNull
    public String getAudio() {
        File file = new File(getRootPath() + AUDIO);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + AUDIO;
    }

    @NonNull
    public String getTmp() {
        File file = new File(getRootPath() + TMP);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + TMP;
    }

    @NonNull
    public String getDownload() {
        File file = new File(getRootPath() + DOWNLOAD);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + DOWNLOAD;
    }

    @NonNull
    public String getLog() {
        File file = new File(getRootPath() + LOG);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + LOG;
    }

    @NonNull
    public String getHeadFile() {
        File file = new File(getRootPath() + HEADFILE);
        if (!file.exists()) {
            file.mkdirs();
        }
        return getRootPath() + HEADFILE;
    }

    public void mkdirs() {
        Log.i(TAG, "---mkdirs===");
        File file = new File(getRootPath());
        if (!file.exists()) {
            file.mkdirs();
            Log.i(TAG, "---mkdirs===" + file.getPath());
        }
      /*file = new File(getImg());
      if(!file.exists())
				file.mkdirs();

			file = new File(getImg());
			if(!file.exists())
				file.mkdirs();

			file = new File(getTmp());
			if(!file.exists())
				file.mkdirs();

			file = new File(getDownload());
			if(!file.exists())
				file.mkdirs();*/
    }

    public void delete(String dir, FilenameFilter filter) {
        if (TextUtils.isEmpty(dir)) {
            return;
        }
        File file = new File(dir);
        if (!file.exists()) {
            return;
        }
        if (file.isFile()) {
            file.delete();
        }
        if (!file.isDirectory()) {
            return;
        }

        File[] lists ;
        if (filter != null) {
            lists = file.listFiles(filter);
        } else {
            lists = file.listFiles();
        }

        if (lists == null) {
            return;
        }
        for (File f : lists) {
            if (f.isFile()) {
                f.delete();
            }
        }
    }
}
